//
//  myViewController.m
//  HGY
//
//  Created by lanou3g on 15/11/3.
//  Copyright © 2015年 顾成辉. All rights reserved.
//

#import "myViewController.h"
#import "sqManager.h"
#import "thirdSQManager.h"
#import "DetailViewController.h"
#import "RequestViewController.h"
#import "VideoViewController.h"
@interface myViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *array1;
@end

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTableview];
    
    [self actionButton];
}

- (void)addTableview
{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section == 1) {
//        NSMutableArray *array = [[sqFenLei shareManager] queryAll];
//        return array.count;
//        
//    } else  if (section == 0){
//        NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
//
//        return arr1.count;
//    } else if (section == 2){
//    NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
//        return arr3.count;
//    }
//    return 0;
    
    
    NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
    
    NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
    
    NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
    
//    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
//        return 0;
//    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
//        return arr3.count;
//    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
//        return arr2.count;
//    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
//        return arr1.count;
//    } else if (arr1.count == 0) {
//        if (section == 0) {
//            return arr2.count;
//        } else {
//            return arr3.count;
//        }
//    } else if (arr2.count == 0) {
////        NSArray *array = @[@"美食", @"视频"];
////        return array.count;
//        if (section == 0) {
//            return arr1.count;
//        } else {
//            return arr3.count;
//        }
//    } else if (arr3.count == 0) {
//        if (section == 0) {
//            return arr1.count;
//        } else {
//            return arr2.count;
//        }
//    } else {
//        if (section == 0) {
//            return arr1.count;
//        } else if(section == 1){
//            return arr2.count;
//        } else {
//            return arr2.count;
//        }
//    }
    
    NSInteger index = arr1.count + arr2.count + arr3.count;
    return index;
   
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
//    NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
//    
//    NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
//    
//    NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
//    
//    if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
//        return 0;
//    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
//        return 1;
//    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
//        return 1;
//    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
//        return 1;
//    }  if (arr1.count == 0) {
//        return 2;
//    } else if (arr2.count == 0) {
//        return 2;
//    } else if (arr3.count == 0) {
//        return 2;
//    } else {
//        return 3;
//    }
//
    

    return 1;

//    NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
//    
//    NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
//    
//    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
//        return 0;
//    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
//        return 1;
//    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
//        return 1;
//    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
//        return 1;
//    } else if (arr1.count == 0) {
//        return 2;
//    } else if (arr2.count == 0) {
//        return 2;
//    } else if (arr3.count == 0) {
//        return 2;
//    } else {
//        return 3;
//    }
    


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//
////    NSMutableArray * arr1 = [NSMutableArray array];
////    NSMutableArray * arr2 = [NSMutableArray array];
////    NSMutableArray * arr3 = [NSMutableArray array];
////    
////    arr2 = [[sqFenLei shareManager] queryAll];
////    
////    arr3 = [[thirdSQManager shareManager]queryAll];
////    
////    arr1 = [[sqManager shareManager] queryAll];
////    
////    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
////        return nil;
////    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
////        NSArray *array = @[@"视频"];
////        return array[section];
////    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
////        NSArray *array = @[@"分类"];
////        return array[section];
////    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
////        NSArray *array = @[@"美食"];
////        return array[section];
////    }  if (arr1.count == 0) {
////        NSArray *array = @[@"分类", @"视频"];
////        return array[section];
////    } else if (arr2.count == 0) {
////        NSArray *array = @[@"美食", @"视频"];
////        return array[section];
////    } else if (arr3.count == 0) {
////        NSArray *array = @[@"美食", @"分类"];
////        return array[section];
////    } else {
////        NSArray *array = @[@"美食", @"分类", @"视频"];
////        return array[section];
////    }
////
////    NSMutableArray * arr1 = [NSMutableArray array];
////    NSMutableArray * arr2 = [NSMutableArray array];
////    NSMutableArray * arr3 = [NSMutableArray array];
////    
////    arr2 = [[sqFenLei shareManager] queryAll];
////    
////    arr3 = [[thirdSQManager shareManager]queryAll];
////    
////    arr1 = [[sqManager shareManager] queryAll];
////    
////    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
////        return nil;
////    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
////        NSArray *array = @[@"视频"];
////        return array[section];
////    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
////        NSArray *array = @[@"分类"];
////        return array[section];
////    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
////        NSArray *array = @[@"美食"];
////        return array[section];
////    } else if (arr1.count == 0) {
////        NSArray *array = @[@"分类", @"视频"];
////        return array[section];
////    } else if (arr2.count == 0) {
////        NSArray *array = @[@"美食", @"视频"];
////        return array[section];
////    } else if (arr3.count == 0) {
////        NSArray *array = @[@"美食", @"分类"];
////        return array[section];
////    } else {
////        NSArray *array = @[@"美食", @"分类", @"视频"];
////        return array[section];
////    }
//
//    return nil;
//    
//}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
    }
    NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
    
    NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
    
    NSMutableArray *arr1 = [[sqManager shareManager] queryAll];

    MenuModel *model = [[MenuModel alloc] init];
    model = arr1[indexPath.row];
    cell.textLabel.text = model.title;
    
    sqModel *model1 = [[sqModel alloc] init];
    NSInteger index = arr1.count + indexPath.row;
    model1 = arr2[index];
    cell.textLabel.text = model1.title;
    
    ThirdModel *mode2 = [[ThirdModel alloc] init];
    NSInteger index1 = index + indexPath.row;
    mode2 = arr3[index1];
    cell.textLabel.text = mode2.title;
    
    
    
//    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
//        return cell;
//    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
//        if (arr3.count > 0) {
//        ThirdModel *model = [[ThirdModel alloc] init];
//        model = arr3[indexPath.row];
//        cell.textLabel.text = model.title;

    
    
    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
        return cell;
    }
    if (arr1.count == 0 && arr2.count == 0 && arr3.count > 0) {
        if (indexPath.section == 0) {
            ThirdModel *model = arr3[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        }
        return cell;
    }
    if (arr1.count == 0 && arr2.count > 0 && arr3.count > 0) {
        if (indexPath.section == 0) {
            sqModel *model = arr2[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        } else if (indexPath.section == 1) {
            ThirdModel *model = arr3[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        }
        return cell;
    }
    if (arr1.count > 0 && arr2.count == 0 && arr3.count == 0) {
        if (indexPath.section == 0) {
            MenuModel *model = arr1[indexPath.row];
            cell.textLabel.text = model.title;
        }
        return cell;
    }
    if (arr1.count > 0 && arr2.count > 0 && arr3.count == 0) {
        if (indexPath.section == 0) {
            MenuModel *model = arr1[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        } else if (indexPath.section == 1) {
            sqModel *model = arr2[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        }
        return cell;
    }
    if (arr1.count == 0 && arr2.count > 0 && arr3.count == 0) {
        if (indexPath.section == 0) {
            sqModel *model = arr2[indexPath.row];
            cell.textLabel.text = model.title;
        }
        return cell;
    }
    if (arr1.count > 0 && arr2.count == 0 && arr3.count > 0) {
        if (indexPath.section == 0) {
            MenuModel *model = arr1[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        } else if (indexPath.section == 1) {
            ThirdModel *model = arr3[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        }
        return cell;
    }
    if (arr1.count > 0 && arr2.count > 0 && arr3.count > 0) {
        if (indexPath.section == 0) {
            MenuModel *model = arr1[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        } else if (indexPath.section == 1) {
            sqModel *model = arr2[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        } else if (indexPath.section == 2) {
            ThirdModel *model = arr3[indexPath.row];
            cell.textLabel.text = model.title;
            return cell;
        }
        return cell;
    }
    
    
//    if (arr1.count == 0 && arr2.count == 0 && arr3.count == 0) {
//        return nil;
//    } else if (arr1.count == 0 && arr2.count == 0 && arr3.count != 0) {
//        if (arr3.count > 0) {
//        ThirdModel *model = [[ThirdModel alloc] init];
//        model = arr3[indexPath.row];
//        cell.textLabel.text = model.title;
//            return cell;
//        }
//    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
//        if (arr2.count > 0) {
//            sqModel *model = [[sqModel alloc] init];
//            model = arr2[indexPath.row];
//            cell.textLabel.text = model.title;
//        }
//        return cell;
//    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
//        if (arr1.count > 0) {
//            MenuModel *model = [[MenuModel alloc] init];
//            model = arr1[indexPath.row];
//            cell.textLabel.text = model.title;
//        }
//        return cell;
//    }  if (arr1.count == 0) {
////        NSArray *array = @[@"分类", @"视频"];
////        return array[section];
//        if (indexPath.section == 0) {
//            if (arr2.count > 0) {
//                sqModel *model = [[sqModel alloc] init];
//                model = arr2[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr3.count > 0) {
//                ThirdModel *model = [[ThirdModel alloc] init];
//                model = arr3[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        }
//    } else if (arr2.count == 0) {
////        NSArray *array = @[@"美食", @"视频"];
////        return array[section];
//        if (indexPath.section == 0) {
//            if (arr1.count > 0) {
//                MenuModel *model = [[MenuModel alloc] init];
//                model = arr1[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr3.count > 0) {
//                ThirdModel *model = [[ThirdModel alloc] init];
//                model = arr3[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        }
//    } else if (arr3.count == 0) {
//        if (indexPath.section == 0) {
//            if (arr1.count > 0) {
//                MenuModel *model = [[MenuModel alloc] init];
//                model = arr1[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr2.count > 0) {
//                sqModel *model = [[sqModel alloc] init];
//                NSLog(@"--------%ld", indexPath.row);
//                model = arr2[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        }
//    } else {
//        if (indexPath.section == 0) {
//            if (arr1.count > 0) {
//                MenuModel *model = [[MenuModel alloc] init];
//                model = arr1[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else if (indexPath.section == 1) {
//            if (arr2.count > 0) {
//                sqModel *model = [[sqModel alloc] init];
//                model = arr2[indexPath.row];
//                cell.textLabel.text = model.title;
//                return cell;
//            }
//        } else if (indexPath.section == 2) {
//            if (arr3.count > 0) {
//                ThirdModel *model = [[ThirdModel alloc] init];
//                NSLog(@"%ld", indexPath.row);
//                model = arr3[indexPath.row];
//                cell.textLabel.text = model.title;
//                return cell;
//            }
//        }
//    }

//    if (indexPath.section == 0) {
//        NSMutableArray *array8 = [NSMutableArray array];
//        array8 = [[sqManager shareManager] queryAll];
//        if (array8.count > 0) {
//            MenuModel *model = [[MenuModel alloc] init];
//            model = array8[indexPath.row];
//            cell.textLabel.text = model.title;

//        }
//         return cell;
//    } else if (arr1.count == 0 && arr3.count == 0 && arr2.count != 0) {
//        if (arr2.count > 0) {
//            sqModel *model = [[sqModel alloc] init];
//            model = arr2[indexPath.row];
//            cell.textLabel.text = model.title;
//        }
//        return cell;
//    }  else if (arr3.count == 0 && arr2.count == 0 && arr1.count != 0) {
//        if (arr1.count > 0) {
//            MenuModel *model = [[MenuModel alloc] init];
//            model = arr1[indexPath.row];
//            cell.textLabel.text = model.title;
//        }
//        return cell;
//    }  if (arr1.count == 0) {
////        NSArray *array = @[@"分类", @"视频"];
////        return array[section];
//        if (indexPath.section == 0) {
//            if (arr2.count > 0) {
//                sqModel *model = [[sqModel alloc] init];
//                model = arr2[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr3.count > 0) {
//                ThirdModel *model = [[ThirdModel alloc] init];
//                model = arr3[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        }
//    } else if (arr2.count == 0) {
////        NSArray *array = @[@"美食", @"视频"];
////        return array[section];
//        if (indexPath.section == 0) {
//            if (arr1.count > 0) {
//                MenuModel *model = [[MenuModel alloc] init];
//                model = arr1[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr3.count > 0) {
//                ThirdModel *model = [[ThirdModel alloc] init];
//                model = arr3[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        }
//    } else if (arr3.count == 0) {
//        if (indexPath.section == 0) {
//            if (arr1.count > 0) {
//                MenuModel *model = [[MenuModel alloc] init];
//                model = arr1[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr2.count > 0) {
//                sqModel *model = [[sqModel alloc] init];
//                NSLog(@"--------%ld", indexPath.row);
//                model = arr2[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        }
//    } else {
//        if (indexPath.section == 0) {
//            if (arr1.count > 0) {
//                MenuModel *model = [[MenuModel alloc] init];
//                model = arr1[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else if (indexPath.section == 1) {
//            if (arr2.count > 0) {
//                sqModel *model = [[sqModel alloc] init];
//                model = arr2[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//            return cell;
//        } else {
//            if (arr3.count > 0) {
//                ThirdModel *model = [[ThirdModel alloc] init];
//                model = arr3[indexPath.row];
//                cell.textLabel.text = model.title;
//            }
//        }
//    }


    return cell;
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *array1 = [[sqManager shareManager] queryAll];
    NSMutableArray *array2 = [[sqFenLei shareManager] queryAll];
    NSMutableArray *array3 = [[thirdSQManager shareManager] queryAll];
    if (indexPath.section == 0) {
        if (array1.count == 0) {
            if (array2.count != 0) {
                sqModel *model = [[sqModel alloc] init];
                model = array2[indexPath.row];
                RequestViewController *vc = [[RequestViewController alloc] init];
                vc.requestUrl = model.deal_h5_url;
                [self.navigationController pushViewController:vc animated:YES];
                return;

            } else {
                if (array3.count == 0) {
                    return ;
                }
                ThirdModel *model = [[ThirdModel alloc] init];
                model = array3[indexPath.row];
                VideoViewController *vc = [[VideoViewController alloc] init];
                vc.thirdmodel = model;
                [self.navigationController pushViewController:vc animated:YES];
                return;
            }
           
        }
        MenuModel *model = [[MenuModel alloc] init];
        model = array1[indexPath.row];
        DetailViewController *vc = [[DetailViewController alloc] init];
        vc.model = model;
        [self.navigationController pushViewController:vc animated:YES];
        return;
        
    } else if (indexPath.section == 1) {
        
        if (array2.count == 0) {
            if (array3.count == 0) {
                return ;
            }
            ThirdModel *model = [[ThirdModel alloc] init];
            model = array3[indexPath.row];
            VideoViewController *vc = [[VideoViewController alloc] init];
            vc.thirdmodel = model;
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        sqModel *model = [[sqModel alloc] init];
        model = array2[indexPath.row];
        RequestViewController *vc = [[RequestViewController alloc] init];
        vc.requestUrl = model.deal_h5_url;
        [self.navigationController pushViewController:vc animated:YES];
        return;
        
    } else {
        
        if (array3.count == 0) {
            return ;
        }
        ThirdModel *model = [[ThirdModel alloc] init];
        model = array3[indexPath.row];
        VideoViewController *vc = [[VideoViewController alloc] init];
        vc.thirdmodel = model;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }


}

// 编辑触发按钮
- (void)actionButton
{
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionButton:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
}

- (void)actionButton:(UIBarButtonItem *)button
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    if (self.tableView.editing == YES) {
        self.navigationItem.rightBarButtonItem.title = @"完成";
    } else {
    self.navigationItem.rightBarButtonItem.title = @"编辑";
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 编辑删除
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}





 // 编辑的第四步
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
//        
//        NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
//        
//        NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
    
//        if (arr1.count == 0 && arr2.count == 0 && arr3.count > 0) {
//            if (indexPath.section == 0) {
//                ThirdModel *model = arr3[indexPath.row];
//                [[thirdSQManager shareManager] deleteStudentWithString:model.title];
//                [[thirdSQManager shareManager] queryAll];
//            }
//        }
//        if (arr1.count == 0 && arr2.count > 0 && arr3.count > 0) {
//            if (indexPath.section == 0) {
//                sqModel *model = arr2[indexPath.row];
//                [[sqFenLei shareManager] deleteStudentWithString:model.title];
//                [[sqFenLei shareManager] queryAll];
//            } else if (indexPath.section == 1) {
//                ThirdModel *model = arr3[indexPath.row];
//                [[thirdSQManager shareManager] deleteStudentWithString:model.title];
//                [[thirdSQManager shareManager] queryAll];
//            }
//        }
//        if (arr1.count > 0 && arr2.count == 0 && arr3.count == 0) {
//            if (indexPath.section == 0) {
//                MenuModel *model = arr1[indexPath.row];
//                [[sqManager shareManager] deleteStudentWithString:model.title];
//                [[sqManager shareManager] queryAll];
//            }
//        }
//        if (arr1.count > 0 && arr2.count > 0 && arr3.count == 0) {
//            if (indexPath.section == 0) {
//                MenuModel *model = arr1[indexPath.row];
//                [[sqManager shareManager] deleteStudentWithString:model.title];
//                [[sqManager shareManager] queryAll];
//            } else if (indexPath.section == 1) {
//                sqModel *model = arr2[indexPath.row];
//                [[sqFenLei shareManager] deleteStudentWithString:model.title];
//                [[sqFenLei shareManager] queryAll];
//            }
//        }
//        if (arr1.count == 0 && arr2.count > 0 && arr3.count == 0) {
//            if (indexPath.section == 0) {
//                sqModel *model = arr2[indexPath.row];
//                [[sqFenLei shareManager] deleteStudentWithString:model.title];
//                [[sqFenLei shareManager] queryAll];
//            }
//        }
//        if (arr1.count > 0 && arr2.count == 0 && arr3.count > 0) {
//            if (indexPath.section == 0) {
//                MenuModel *model = arr1[indexPath.row];
//                [[sqManager shareManager] deleteStudentWithString:model.title];
//                [[sqManager shareManager] queryAll];
//            } else if (indexPath.section == 1) {
//                ThirdModel *model = arr3[indexPath.row];
//                [[thirdSQManager shareManager] deleteStudentWithString:model.title];
//                [[thirdSQManager shareManager] queryAll];
//            }
//        }
//        if (arr1.count > 0 && arr2.count > 0 && arr3.count > 0) {
//            if (indexPath.section == 0) {
//                MenuModel *model = arr1[indexPath.row];
//                [[sqManager shareManager] deleteStudentWithString:model.title];
//                [[sqManager shareManager] queryAll];
//            } else if (indexPath.section == 1) {
//                sqModel *model = arr2[indexPath.row];
//                [[sqFenLei shareManager] deleteStudentWithString:model.title];
//                [[sqFenLei shareManager] queryAll];
//            } else if (indexPath.section == 2) {
//                ThirdModel *model = arr3[indexPath.row];
//                [[thirdSQManager shareManager] deleteStudentWithString:model.title];
//                [[thirdSQManager shareManager] queryAll];
//            }
//        }
//
//    }
//    
    
    
        if (indexPath.section == 0) {
             if (editingStyle == UITableViewCellEditingStyleDelete) {
            NSMutableArray *array4 = [NSMutableArray array];
            array4 = [[sqManager shareManager] queryAll];
            if (array4.count != 0) {
                NSMutableArray *array2 = [NSMutableArray array];
                array2 = [[sqFenLei shareManager] queryAll];
                sqModel *model = [[sqModel alloc] init];
                NSLog(@"%@", array2);
                model = array2 [indexPath.row];
                [[sqFenLei shareManager] deleteStudentWithString:model.title];
                [self.tableView reloadData];
                if (array2.count != 0) {
                    NSMutableArray *array1 = [NSMutableArray array];
                    array1 = [[thirdSQManager shareManager] queryAll];
                    if (array1.count == 0) {
                        return ;
                              }
                    
                    ThirdModel *model = [[ThirdModel alloc] init];
                    model = array1[indexPath.row];
                    [[thirdSQManager shareManager] deleteStudentWithString:model.title];
                    [self.tableView reloadData];
                    return;
                }
               return;
            }
            
            MenuModel *model = [[MenuModel alloc] init];
            model = array4[indexPath.row];
            [[sqManager shareManager] deleteStudentWithString:model.title];
                 [self.tableView reloadData];
           }
        } else if (indexPath.section == 1) {
             if (editingStyle == UITableViewCellEditingStyleDelete) {
            NSMutableArray *array2 = [NSMutableArray array];
            array2 = [[sqFenLei shareManager] queryAll];
            
            if (array2.count != 0) {
                NSMutableArray *array1 = [NSMutableArray array];
                array1 = [[thirdSQManager shareManager] queryAll];
                if (array1.count == 0) {
                    return ;
                }
                
                ThirdModel *model = [[ThirdModel alloc] init];
                model = array1[indexPath.row];
                [[thirdSQManager shareManager] deleteStudentWithString:model.title];
                [self.tableView reloadData];
                return;
            }

        sqModel *model = [[sqModel alloc] init];
            model = array2 [indexPath.row];
        [[sqFenLei shareManager] deleteStudentWithString:model.title];
            
        [self.tableView reloadData];
            return;

          }
        } else if (indexPath.section == 2) {
             if (editingStyle == UITableViewCellEditingStyleDelete) {
            NSMutableArray *array1 = [NSMutableArray array];
            array1 = [[thirdSQManager shareManager] queryAll];
            if (array1.count == 0) {
                return ;
            }

        ThirdModel *model = [[ThirdModel alloc] init];
        model = array1[indexPath.row];
    [[thirdSQManager shareManager] deleteStudentWithString:model.title];
        [self.tableView reloadData];
            return;
        }

}

    return;
   
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (sourceIndexPath.section == destinationIndexPath.section) {
        if (sourceIndexPath.section == 0) {
           NSMutableArray *array = [[sqManager shareManager] queryAll];
             MenuModel *model = [[MenuModel alloc] init];
            model = array[sourceIndexPath.row];
            [array removeObjectAtIndex:sourceIndexPath.row];
            [array insertObject:model atIndex:destinationIndexPath.row];
            [self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        }
    }
}


- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{

    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
